package com.hongyi.modelcontest.core.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

    private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);
    /**
     * 完成的结果文件--输出的压缩文件
     */
    private File targetFile;

    public ZipUtils(String target) {
        targetFile = new File(target);
        if (targetFile.exists()) {
            targetFile.delete();
        }
    }

    /**
     * 压缩文件
     */
    public void zipFiles(String srcFileName) {
        File srcFile = new File(srcFileName);
        ZipOutputStream out = null;
        try {
            out = new ZipOutputStream(new FileOutputStream(targetFile));
            if (srcFile.isFile()) {
                zipFile(srcFile, out, "");
            } else {
                File[] list = srcFile.listFiles();
                if (list != null) {
                    for (File aList : list) {
                        compress(aList, out, "");
                    }
                }
            }
            logger.info("压缩完毕");
        } catch (Exception e) {
            logger.error("压缩异常:{}", e);
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
                logger.error("压缩异常:{}", e);
            }
        }
    }

    /**
     * 压缩文件夹里的文件
     * 起初不知道是文件还是文件夹--- 统一调用该方法
     */
    private void compress(File file, ZipOutputStream out, String basedir) {
        /* 判断是目录还是文件 */
        if (file.isDirectory()) {
            this.zipDirectory(file, out, basedir);
        } else {
            this.zipFile(file, out, basedir);
        }
    }

    /**
     * 压缩单个文件
     */
    private void zipFile(File srcfile, ZipOutputStream out, String basedir) {
        if (!srcfile.exists())
            return;

        byte[] buf = new byte[1024];
        FileInputStream in = null;

        try {
            int len;
            in = new FileInputStream(srcfile);
            out.putNextEntry(new ZipEntry(basedir + srcfile.getName()));

            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } catch (Exception e) {
            logger.error("压缩异常:{}", e);
        } finally {
            try {
                if (out != null)
                    out.closeEntry();
                if (in != null)
                    in.close();
            } catch (IOException e) {
                logger.error("压缩异常:{}", e);
            }
        }
    }

    /**
     * 压缩文件夹
     */
    private void zipDirectory(File dir, ZipOutputStream out, String basedir) {
        if (!dir.exists())
            return;
        File[] files = dir.listFiles();
        for (File file : files != null ? files : new File[0]) {
            /* 递归 */
            compress(file, out, basedir + dir.getName() + "/");
        }
    }

}
